package com.hujia.hjsurprisemeal.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hujia.hjsurprisemeal.model.Feedback;
import com.hujia.hjsurprisemeal.repository.FeedbackMapper;
import com.hujia.hjsurprisemeal.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public int addFeedback(Feedback feedback) {
        return feedbackMapper.insert(feedback);
    }

    @Override
    public List<Feedback> getAllFeedbacks() {
        return feedbackMapper.selectList(null);
    }

    @Override
    public Feedback getFeedbackById(int id) {
        return feedbackMapper.selectById(id);
    }

    @Override
    public int updateFeedback(Feedback feedback) {
        return feedbackMapper.updateById(feedback);
    }

    @Override
    public int deleteFeedback(int id) {
        return feedbackMapper.deleteById(id);
    }

    // 额外的方法，使用QueryWrapper进行条件查询
    public List<Feedback> getFeedbacksByUserId(int userId) {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return feedbackMapper.selectList(queryWrapper);
    }

    // 分页查询方法
    public Page<Feedback> getFeedbacksByPage(int currentPage, int pageSize) {
        Page<Feedback> page = new Page<>(currentPage, pageSize);
        return feedbackMapper.selectPage(page, null);
    }
}